package com.garen.utils;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * 语法上的补充
 * @author Yuanwen
 *
 */
public class LangUtils {
	 
	
	public static int getIntValue(JSONObject jobj,String key){
		Integer x = null;
		try{
			x = jobj.getInteger(key);
		}catch(Exception ex){
			x = 0;
		}
		return x == null?0:x;
	}
	
	public static long getLongValue(JSONObject jobj,String key){
		Long x = getLong(jobj,key);
		return x == null?0:x;
	}
	
	public static Long getLong(JSONObject jobj,String key){
		Long x = null;
		try{
			x = jobj.getLong(key);
		}catch(Exception ex){
		}
		return x;
	} 
	
	
	public static float getFloatValue(JSONObject jobj,String key){
		Float x = getFloat(jobj,key);
		return x == null?0:x;
	}
	
	public static Float getFloat(JSONObject jobj,String key){
		Float x = null;
		try{
			x = jobj.getFloat(key);
		}catch(Exception ex){}
		return x;
	}
	
	
	//json解析对象
	public static JSONObject getJSONObject(JSONObject jobj,String key) {
		if(jobj == null || key == null) return null;
		try{
			return jobj.getJSONObject(key);
		}catch(Exception ex){}
		return null;
	}

	/*
	 * 
	 */
	public static String log4stack(Exception ex){
		String ret = null;
		if(ex == null) return null;
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(os);
		ex.printStackTrace(ps);
		ret = new String(os.toByteArray());
		return ret;
	}
	/*
	 * 逗号分割的字符串,转化为set集合,flag true 转化为小写,false不转化
	 * */
	public static Set<String> str2Set(String strs){
		return str2Set(strs,true);
	}
	
	/*
	 * 逗号分割的字符串,转化为set集合,flag true 转化为小写,false不转化
	 * */
	public static Set<String> str2Set(String strs,boolean flag){
		Set<String> set = new HashSet<String>();
		if(StringUtils.isEmpty(strs)) return set;
		String [] aStrs = strs.split(",");
		for(String str : aStrs){
			if(StringUtils.isEmpty(str)) continue;
			if(flag) str = str.toLowerCase();
			set.add(str.trim());
		}
		return set;
	}
	
	/*
	 * list数据转换逗号分割字符串
	 */
	public static String list2Str(List<? extends Object> list){
		StringBuilder sbStr = new StringBuilder();
		for(Object obj : list){
			if(sbStr.length() > 0)
				sbStr.append(",");
			sbStr.append(obj.toString());
		}
		return sbStr.toString();
	}
	
	/*
	 * 判断list是否为null或size == 0
	 * */
	public static boolean listIsNotNull(List<? extends Object> list){
		if(list==null) return false;
		else if(list.size() == 0) return false;
		return true;
	}
	
	/*
	 * 判断list是否为null或size == 0
	 * */
	public static boolean listIsNull(List<? extends Object> list){
		return !listIsNotNull(list);
	}
	
	
	/**
	  * 逗号分割的字符串，转换成string list
	  * @param strs
	  * @return
	  */
   public static  List<String> str2StrList(String strs){
       List<String> strlist = new ArrayList<String>();
       if(strs==null || "".equals(strs)) return strlist;
       for(String str : strs.split(",")){
           strlist.add(str);
       }
       return strlist;
   }

   /**
	  * 逗号分割的字符串，转换成Integer list
	  * @param strs
	  * @return
	  */
   public static  List<Integer> str2IntList(String strs){
       List<Integer> intlist = new ArrayList<Integer>();
       if(strs==null || "".equals(strs)) return intlist;
       for(String str : strs.split(",")){
           intlist.add(s2int(str));
       }
       return intlist;
   }
   
   /**
	  * 逗号分割的字符串，转换成long list
	  * @param strs
	  * @return
	  */
  public static  List<Long> str2longList(String strs){
      List<Long> intlist = new ArrayList<Long>();
      if(strs==null || "".equals(strs)) return intlist;
      for(String str : strs.split(",")){
          intlist.add(s2l(str));
      }
      return intlist;
  }
  
  
  /**
	 * 字符串数字格式化，错误返回0.
	 * @param snum
	 * @return
	 */
	public static int s2int(Object snum){
		int num = 0;
		if(snum == null) return num;
		try{
			num = Integer.parseInt(snum.toString());
		}catch(NumberFormatException ex){
		}
		return num;
	}
	
	/**
	 * 字符串数字格式化，错误返回0.
	 * @param snum
	 * @return
	 */
	public static Long s2l(String snum){
		Long num = 0L;
		if(snum == null) return num;
		try{
			num = Long.parseLong(snum);
		}catch(NumberFormatException ex){
		}
		return num;
	}
	
}
